import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex'
const store = useStore();
const http = axios.create({
    timeout:10000,
    baseURL:'/api'
})

http.interceptors.request.use(config => {
    return config;
},err => {
    Promise.reject(err);
})

http.interceptors.response.use(res => {
    console.log(res, 11)
    let status = res.data.code;
    let msg = res.data.msg;
    if (msg == "无效token") {
        ElMessageBox.confirm('token过期，是否跳转到登录页？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            location.href = "/login"
        }).catch(() => {

        })
    } else if (status == 400 || status == 401 || status == 403 || status == 404 || status == 500) {
        ElMessage.error({
            duration: 1000,
            message: msg
        })
    }
    return res.data;
},err => {
    let { message } = err; //结构出错误信息
    if (message == "Error") {
        message = "后端接口连接Network异常";
    }
    if (message.includes('timeout')) {
        message = "系统接口请求超时";
    }
    ElMessage.error({
        duration: 1000,
        message: message
    })
    Promise.reject(err)
})


export default http